package com.knife.bbs.domain;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.knife.dbo.EasyJDB;
import com.knife.dbo.IObject;
import com.knife.bbs.business.BBSUserService;


public class BBSScore implements IObject {
	private String cid;
	private String username;
	private Integer score;
	private Date vdate;
	// 关于type的定义：0-10公共活动参得分。发帖pm(0)、回帖rm(1)、点击click(2)、发表文章pa(3)，发表评论ra(5)，参与投票vote（6）
	// 11-20项目活动参与分:推荐会员introUser(11)、推荐网址introSite(12)、introOther其它推广(13)、work项目成员(16)、other其它
	// 21-30其它加分
	private String types;
	private String intro;
	private Integer status;

	private static BBSUserService userService = BBSUserService.getInstance();

	public static BBSScore read(String cid) {
		EasyJDB db = EasyJDB.getInstance();
		return (BBSScore) db.get(BBSScore.class, cid);
	}

	public static BBSScore addScore(String userName, Score score, String intro) {
		if (score == null)
			return null;
		BBSScore s = new BBSScore();
		s.setUsername(userName);
		s.setScore(new Integer(score.getValue()));
		s.setVdate(new Date());
		s.setTypes(score.getName());
		s.setIntro(intro);
		s.setStatus(new Integer(0));
		if (s.save()) {
			com.knife.bbs.object.UserInfo user = userService.getUserInfoByName(userName);
			if (user != null) {
				UserInfo auser=new UserInfo();
				//auser.setUserInfo(user);
				user.setScore(new Integer(score.getValue()
						+ (user.getScore() == null ? 0 : user.getScore()
								.intValue())));
				auser.setUserInfo(user);
				userService.update(auser);
			}
			return s;
		} else
			return null;
	}

	public static BBSScore addScore(String userName, int score, String types,
			String intro) {
		BBSScore s = new BBSScore();
		s.setUsername(userName);
		s.setScore(new Integer(score));
		s.setVdate(new Date());
		s.setTypes(types);
		s.setIntro(intro);
		s.setStatus(new Integer(0));
		if (s.save()) {// 总分增加
			com.knife.bbs.object.UserInfo user = userService.getUserInfoByName(userName);
			if (user != null) {
				UserInfo auser=new UserInfo();
				user.setScore(new Integer(score + user.getScore().intValue()));
				auser.setUserInfo(user);
				userService.update(auser);
			}
			return s;
		} else
			return null;
	}

	public static List query(String scope) {
		EasyJDB db = EasyJDB.getInstance();
		return db.query(BBSScore.class, scope);
	}

	public static List query(String scope, Collection paras) {
		EasyJDB db = EasyJDB.getInstance();
		return db.query(BBSScore.class, scope, paras);
	}

	public boolean save() {
		EasyJDB db = EasyJDB.getInstance();
		boolean ret = db.saveOrUpdate(this);
		return ret;
	}

	public boolean update() {
		EasyJDB db = EasyJDB.getInstance();
		boolean ret = db.update(this);
		return ret;
	}

	public boolean del() {
		EasyJDB db = EasyJDB.getInstance();
		boolean ret = db.del(this);
		if (ret) {
			com.knife.bbs.object.UserInfo user = userService.getUserInfoByName(username);
			if (user != null) {
				UserInfo auser=new UserInfo();
				user.setScore(new Integer(user.getScore().intValue()
						- this.getScore().intValue()));
				auser.setUserInfo(user);
				userService.update(auser);
			}
		}
		return ret;
	}

	public String getTableName() {
		// TODO Auto-generated method stub
		return "BBSScore";
	}

	public String getKeyField() {
		// TODO Auto-generated method stub
		return "cid";
	}

	public String getKeyGenerator() {
		// TODO Auto-generated method stub
		return "com.knife.dbo.RandomIdGenerator";
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getTypes() {
		return types;
	}

	public void setTypes(String types) {
		this.types = types;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String userName) {
		this.username = userName;
	}

	public Date getVdate() {
		return vdate;
	}

	public void setVdate(Date vdate) {
		this.vdate = vdate;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
